package com.huichenghe.xinlvsh01.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.huichenghe.xinlvsh01.CommonUtilss.CommonUtils;
import com.huichenghe.xinlvsh01.R;

import java.util.ArrayList;

/**
 * 展示睡眠数据的自定义ImageView
 * Created by lixiaoning on 15-12-7.
 */
public class SleepDetaillView extends ImageView
{
    public static final String TAG = SleepDetaillView.class.getSimpleName();

    private static final int textSizeInDp = 16;
    private float textHeightInPx;

    private ArrayList<StData> mAllData = new ArrayList<StData>();
    int mMinTime, mMaxTime;
    int indexCount = 0;
    private String beginTime, endTime;
    private Context mContext;


    private class StData
    {
        // 一个int数据代表十分钟的睡眠状态
        String sleepTimeBegin;
        private int[] sleepStatus;
    }

    public void setSleepData(String beginTime, String endTime, String sleepStatus)
    {
        this.beginTime = beginTime;
        this.endTime = endTime;
        StData stData = new StData();
        stData.sleepTimeBegin = beginTime;

        stData.sleepStatus = new int[sleepStatus.length()];

        Log.i(TAG, "开始和结束时间：" + beginTime + "--" + endTime);



        int length = sleepStatus.length();

        // 遍历String并将每一个String转化为int，同时赋值给int数组
        for (int i = 0; i < length; i++)
        {
            String sub = sleepStatus.substring(i, i + 1);
            stData.sleepStatus[i] = Integer.parseInt(sub);

            Log.i(TAG, "睡眠数据：：：" + sub);
            indexCount ++;
        }

        mAllData.add(stData);

    }




    public SleepDetaillView(Context context)
    {
        super(context);
        this.mContext = context;
        textHeightInPx = CommonUtils.getTextViewWidth(context, "A", textSizeInDp);
    }

    public SleepDetaillView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.mContext = context;
        textHeightInPx = CommonUtils.getTextViewWidth(context, "A", textSizeInDp);
    }

    public SleepDetaillView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        textHeightInPx = CommonUtils.getTextViewWidth(context, "A", textSizeInDp);
    }


    @Override
    protected void onDraw(Canvas canvas)
    {
        subDraw(canvas);
        super.onDraw(canvas);

    }

    private void subDraw(Canvas canvas)
    {
        Paint paint = new Paint();
        paint.setAntiAlias(true);   // 消除锯齿
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        paint.setTextSize(textHeightInPx);

        float width = getMeasuredWidth();       // 获取当前控件的宽度px
        float height = getMeasuredHeight();

        drawSleepRect(canvas, paint, width, height - textHeightInPx); // 画出睡眠状态
        drawText(canvas, paint, height, width);             // 画出开始和结束时间



    }


    /**
     *
     * @param canvas
     * @param paint
     */
    private void drawText(Canvas canvas, Paint paint, float height, float width)
    {
        paint.setTextSize(textHeightInPx);
        paint.setColor(getResources().getColor(R.color.White_forme));
        canvas.drawText(beginTime, 1, height, paint);

        float aa = CommonUtils.getTextViewWidth(mContext, endTime, textSizeInDp);
        canvas.drawText(endTime, width - aa, height, paint);


    }


    // 此方法用于画出睡眠状态
    private void drawSleepRect(Canvas canvas, Paint paint, float width, float height)
    {
        int allTime = indexCount;
//        allTime /= 60;

        float eachDistanceW = width / allTime;  // 计算每个像素显示多少数据
        float deepSleepH = height;

        int ws = 0;
        float step_x, step_y = height;
        float x = 0;

        for (int i = 0; i < mAllData.size(); i ++)
        {
            for (int j = 0; j < mAllData.get(i).sleepStatus.length; j ++)
            {
                step_x = eachDistanceW * ws;
                ws ++;
                // 一个数字代表十分钟的睡眠状态
                if(mAllData.get(i).sleepStatus[j] == 0 || mAllData.get(i).sleepStatus[j] == 3) // 代表活动
                {
                    x = height/2 - textHeightInPx * 2;
                    paint.setColor(getResources().getColor(R.color.sober));
                }
                else if(mAllData.get(i).sleepStatus[j] == 1)    // 代表浅睡
                {
                    x = height/2 + textHeightInPx * 2;
                    paint.setColor(getResources().getColor(R.color.light_sleep));
                }
                else if(mAllData.get(i).sleepStatus[j] == 2)    // 代表深睡
                {
                    x = 0;
                    paint.setColor(getResources().getColor(R.color.deep_sleep));
                }

                canvas.drawRect(step_x, x, step_x + eachDistanceW, step_y, paint);

            }
        }
    }
}
